//
//  DDTabBar.swift
//  doudou_weibo
//
//  Created by apple on 2016/11/12.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class DDTabBar: UITabBar {

    //自定义闭包用户撰写按钮的回调
    var composeClosure: (() -> ())?
    
    
    //重写构造方法添加中间的撰写按钮
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        addSubview(composeButton)
        composeButton.addTarget(self, action: #selector(clickComposeButton), for: .touchUpInside)
    }
    
    
    @objc private func clickComposeButton() {
        
        print(#function,"点击了撰写按钮,进行闭包回调")
        
    }
    
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        addSubview(composeButton)
        composeButton.addTarget(self, action: #selector(clickComposeButton), for: .touchUpInside)
    }
    
    
    
    //注意懒加载的花括弧后面要加小括弧
    lazy var composeButton: UIButton = {
        
        let button = UIButton()
        
        button.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        button.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        button.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        button.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        return button
    }()
    
    
    
    //重写layoutSubViews布局子控件
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let width = UIScreen.main.bounds.width / 5
        let height = self.bounds.height
        var index = 0
        for subView in subviews{
            
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                subView.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: height)
                
                index += (index == 1 ? 2 : 1)
            }
        }
        
        composeButton.frame = CGRect(x: width * 2, y: 0, width: width, height: height)
        
        
    }
    
    
    
    
    
    
    
    
    
    
    

}
